import tkinter as tk
from PIL import Image, ImageTk

import requests

class CompletedQuestionnaireFrame(tk.Toplevel):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent
        self.title("已完成问卷详情")

        # 获取屏幕的宽和高
        sreen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()

        # 设置窗口的宽和高
        root_width = int(sreen_width / 3)
        root_height = int(screen_height / 3)

        # 使窗口居中
        self.geometry("{}x{}+{}+{}".format(
            root_width, root_height,
            int((sreen_width - root_width) / 2), int((screen_height - root_height) / 2)
        ))



        # 使用 Pillow 加载图像
        image = Image.open("Image/return.png")
        # 调整图像大小
        new_size = (25, 25)  # 设置新的尺寸 (宽度, 高度)
        resized_image = image.resize(new_size, Image.LANCZOS)
        photo = ImageTk.PhotoImage(resized_image)

        self.back_button = tk.Button(self,image=photo ,text='返回', compound='left',command=self.back_to_main)
        self.back_button.photo = photo  # 保存图像对象的引用
        self.back_button.place(relx=0.01,rely=0.01)



####
        self.setup_ui()
        # self.load_questionnaires()

    def setup_ui(self):
        # self.main_frame = tk.Frame(self)
        # self.main_frame.place()
        #
        #
        # # Configure the root grid
        # self.grid_rowconfigure(1, weight=1)
        # self.grid_columnconfigure(1, weight=1)
        #
        # # Configure the main_frame grid
        # self.main_frame.grid_rowconfigure(0, weight=1)
        # self.main_frame.grid_rowconfigure(1, weight=0)
        # self.main_frame.grid_columnconfigure(0, weight=1)




        self.text_area = tk.Text(self, wrap=tk.WORD, state=tk.DISABLED)
        self.text_area.place(relx=0.01,rely=0.15,relwidth=0.98,relheight=0.8)
        # self.refresh_button = tk.Button(self.main_frame, text="Refresh", command=self.load_questionnaires)
        # self.refresh_button.grid(row=1, column=0, pady=10, sticky='ew')

    # def load_questionnaires(self):
    #     try:
    #         response = requests.get("http://127.0.0.1:5000/questionnaires")
    #         response.raise_for_status()
    #         questionnaires = response.json()
    #         self.display_questionnaires(questionnaires)
    #     except requests.RequestException as e:
    #         print(f"Error fetching data: {e}")
    #         self.display_error("Error fetching data from server.")
    #
    # def display_questionnaires(self, questionnaires):
    #     self.text_area.config(state=tk.NORMAL)
    #     self.text_area.delete(1.0, tk.END)
    #     for q in questionnaires:
    #         question = f"Q: {q['question']}\n"
    #         answer = f"A: {q['answer']}\n"
    #         self.text_area.insert(tk.END, question)
    #         self.text_area.insert(tk.END, answer)
    #         self.text_area.insert(tk.END, "\n")
    #     self.text_area.config(state=tk.DISABLED)
    #
    # def display_error(self, message):
    #     self.text_area.config(state=tk.NORMAL)
    #     self.text_area.delete(1.0, tk.END)
    #     self.text_area.insert(tk.END, message)
    #     self.text_area.config(state=tk.DISABLED)


#####
    def back_to_main(self):
        self.destroy()
        self.parent.deiconify()